// 该mixin文件配合chart-base.js使用
import { mapState } from 'vuex';
import { getVmPageStatus } from '@/utils/util';

export default {
  data() {
    return {
      winContainerPid: '', // 当前组件的父级弹窗组件id
    };
  },
  computed: {
    ...mapState({
      winContainers: state => state.VisualComponentsModule.winContainers,
      curWinContainerWid: state => state.VisualComponentsModule.curWinContainerWid,
    }),
  },
  watch: {

    // 弹窗打开时，需要重新渲染弹窗下面的组件，防止echart组件不显示
    curWinContainerWid(wid) {
      if (wid && this.winContainerPid === wid) {
        const pageStatus = getVmPageStatus();
        this.loadWidget(pageStatus);
      }
    },
  },
  created() {
    this.bindWinContainer();
  },
  methods: {

    // 绑定弹窗组件关系，用于echart类重新刷新
    bindWinContainer() {
      if (!this.winContainers.length) {
        return;
      }
      let p = this;
      while (p.$parent) {
        p = p.$parent;
        if (p.isWinContainer) {
          this.winContainerPid = p.widgetInstanceId;
          break;
        }
      }
    },
  },
};
